
package Engine.Character;

import java.io.IOException;
import javax.microedition.lcdui.Image;

/**
 * Model the character in a battle. It has the sequences of forward movement, attack and retreat
 * @author Gerardo Galíndez
 */
public class BattleCharacter extends Character {

    //---------------------------------------------------------
    //Movement sequences---------------------------------------
    private final int[] standSequence = {0, 1};
    private final int[] movementSequence = {2};
    private final int[] attackSequence = {3, 4};
    private final int[] retreatSequence = {5};
    private int[] currentSequence = {};
    //Counter of the frames for movement-----------------------
    private int movementCounter;
    //Nnumber of steps per frame-------------------------------
    private final int FRAME_LIMITER = 9;
    //Experience Points----------------------------------------
    private int XP;
    //---------------------------------------------------------
    /**
     * Integer index code representing lyn character
     */
    public static final int LYN = 0;
    /**
     * Integer index code representing eliwood character
     */
    public static final int ELIWOOD = 1;
    /**
     * Integer index code representing eirik character
     */
    public static final int EIRIK = 2;

    /**
     * Creates a battle character depending on the characterCode, with an image, frame dimensions and sepcific element
     * @param characterCode
     */
    public BattleCharacter(int characterCode) {
        super(getImage(characterCode), getFrameWidth(characterCode), getFrameHeight(characterCode), getElement(characterCode));
        this.movementCounter = 0;
        this.setFrameSequence(this.standSequence);
        this.setCurrentPhysicalAttack(characterCode);
        this.setCurrentPhysicalDefense(characterCode);
    }

    // Gets the image of the respective character
    private static Image getImage(int characterCode) {
        try {
            switch (characterCode) {
                case LYN:
                    System.out.println("Creating image");
                    return Image.createImage("/Images/Players/Lyn-Battle.png");
                case ELIWOOD:
                    return Image.createImage("/Images/Players/Eliwood-Battle.png");
                case EIRIK:
                    return Image.createImage("/Images/Players/Eirik-Battle.png");
                default:
                    System.out.println("Incorrect character code.");
                    return null;
            }
        } catch (IOException ioex) {
            ioex.printStackTrace();
            return null;
        }
    }

    // Gets the width of the respective character
    private static int getFrameWidth(int characterCode) {
        switch(characterCode) {
            case LYN:
                return 63;
            case ELIWOOD:
                return 63;
            case EIRIK:
                return 53;
            default:
                System.out.println("Bad width.");
                return -1;
        }
    }

    // Gets the height of the respective character
    private static int getFrameHeight(int characterCode) {
        switch (characterCode) {
            case LYN:
                return 49;
            case ELIWOOD:
                return 55;
            case EIRIK:
                return 41;
            default:
                System.out.println("Bad height.");
                return -1;
        }
    }

    // Gets the element of the respective character
    private static int getElement(int characterCode) {
        switch (characterCode) {
            case LYN:
                return WIND;
            case ELIWOOD:
                return FIRE;
            case EIRIK:
                return WATER;
            default:
                return -1;
        }
    }

    /**
     * Sets the stand sequence
     */
    public void stand() {
        if (currentSequence != this.standSequence) {
            this.setFrameSequence(this.standSequence);
            currentSequence = this.standSequence;
        }
        if (movementCounter > FRAME_LIMITER) {
            super.nextFrame();
            movementCounter = 0;
        } else {
            movementCounter++;
        }
    }

    /**
     * Gets the Character Code
     * @return Integer representing the character code
     */
    public int getCharacterCode() {
        return 0;
    }

    /**
     * Gets the current health points
     * @return Integer representing the character's health points
     */
    public int getCurrentXP(){
        return this.XP;
    }

    /**
     * Gets the current health points according to the parameter XP
     * @param XP
     */
    public void setCurrentXP(int XP) {
        this.XP = XP;
    }

    //Sets the physical attack of the character according to the character
    private void setCurrentPhysicalAttack(int characterCode) {
        switch (characterCode) {
            case LYN:
                super.setPhysicalAttack(30);
            case ELIWOOD:
                super.setPhysicalAttack(40);
            case EIRIK:
                super.setPhysicalAttack(50);
        }
    }

    //Sets the physical defense of the character according to the character
    private void setCurrentPhysicalDefense(int characterCode) {
        switch (characterCode) {
            case LYN:
                super.setPhysicalDefense(20);
            case ELIWOOD:
                super.setPhysicalDefense(30);
            case EIRIK:
                super.setPhysicalDefense(40);
        }
    }

     /**
     * Changes the sequence to attack
     */
    public void attack(){
        if (currentSequence != this.attackSequence) {
            this.setFrameSequence(this.attackSequence);
            currentSequence = this.attackSequence;
        }
        if (movementCounter > FRAME_LIMITER) {
            super.nextFrame();
            movementCounter = 0;
        } else {
            movementCounter++;
        }
    }

    /**
     * Changes the sequence to movement
     */
    public void moveForward(){
        if (currentSequence != this.movementSequence) {
            this.setFrameSequence(this.movementSequence);
            currentSequence = this.movementSequence;
        }
        if (movementCounter > FRAME_LIMITER) {
            super.nextFrame();
            movementCounter = 0;
        } else {
            movementCounter++;
        }
        super.move(-1, 0);
    }

    /**
     * Changes the sequence to retreat
     */
    public void retreat(){
        if (currentSequence != this.retreatSequence) {
            this.setFrameSequence(this.retreatSequence);
            currentSequence = this.retreatSequence;
        }
        if (movementCounter > FRAME_LIMITER) {
            super.nextFrame();
            movementCounter = 0;
        } else {
            movementCounter++;
        }
        super.move(1, 0);
    }
}